package errorex

import "fmt"

type Error struct {
	HttpStatus int         `json:"httpStatus"`
	ErrNo      int         `json:"errno"`
	ErrMsg     string      `json:"errmsg"`
	Data       interface{} `json:"data"`
}

func (e Error) Error() string {
	return fmt.Sprintf("httpStatus:%d errno:%d msg:%s data:%+v", e.HttpStatus, e.ErrNo, e.ErrMsg, e.Data)
}

func (e Error) Response() map[string]interface{} {
	return map[string]interface{}{
		"errno":  e.ErrNo,
		"errmsg": e.ErrMsg,
		"data":   nil,
	}
}

func NewError(httpStatus, errno int) *Error {
	return &Error{
		HttpStatus: httpStatus,
		ErrNo:      errno,
		ErrMsg:     ErrorText(errno),
	}
}

const (
	C_SYSTEM_ERR = -1
	C_OK         = iota
	C_REQ_PARAM_ERR
	C_DATA_HAS_EXSIT_ERR
	C_DATA_NOT_EXSIT_ERR
	C_OVER_LIMIT_ERR
)

var errorCode = map[int]string{
	C_SYSTEM_ERR:         "C_SYSTEM_ERR",
	C_OK:                 "C_OK",
	C_REQ_PARAM_ERR:      "C_REQ_PARAM_ERR",
	C_DATA_HAS_EXSIT_ERR: "C_DATA_HAS_EXSIT_ERR",
	C_DATA_NOT_EXSIT_ERR: "C_DATA_NOT_EXSIT_ERR",
	C_OVER_LIMIT_ERR:     "C_OVER_LIMIT_ERR",
}

var errorText = map[int]string{
	C_SYSTEM_ERR:         "系统内部错误",
	C_OK:                 "",
	C_REQ_PARAM_ERR:      "请求参数错误",
	C_DATA_HAS_EXSIT_ERR: "数据已存在",
	C_DATA_NOT_EXSIT_ERR: "数据不存在",
	C_OVER_LIMIT_ERR:     "请求超过流量限制",
}

func ErrorText(code int) string {
	return errorText[code]
}
